package com.qxp.simplerpc.proxy;

import com.qxp.simplerpc.connect.RpcClient;
import com.qxp.simplerpc.connect.RpcClientHandler;

import java.lang.reflect.Method;
import java.util.UUID;
import java.util.concurrent.*;

/**
 * @author qianxiaopeng
 */
public abstract class AbstractProxy implements IProxy {
    // 客户端
    private RpcClient rpcClient;
    // 客户端句柄
    private RpcClientHandler rpcClientHandler;

    /** 与服务器建立连接
     * @param cls 接口方法
     * @param method 需要执行的方法
     * @param args 方法对应的参数
     * @param className 执行的类名
     * @return  服务器执行结果
     * @throws ExecutionException   执行异常
     * @throws InterruptedException 线程中断异常
     * @throws TimeoutException 客户端获取服务器结果异常
     */
    protected abstract Object connect(Class<?> cls, Method method, Object[] args, String className, String beanName) throws ExecutionException, InterruptedException, TimeoutException;
    // 请求令牌
    protected String getToken() {
        return Thread.currentThread().getName() + UUID.randomUUID().toString();
    }

    public RpcClient getRpcClient() {
        return rpcClient;
    }

    public void setRpcClient(RpcClient rpcClient) {
        this.rpcClient = rpcClient;
    }

    public RpcClientHandler getRpcClientHandler() {
        return rpcClientHandler;
    }

    public void setRpcClientHandler(RpcClientHandler rpcClientHandler) {
        this.rpcClientHandler = rpcClientHandler;
    }
}
